/*Really Simple Reader - An RSS feed reader for Android*/
/*       Created by B. Gokulakrishnan - 080125J        */
/* 		 	 CS3200 - Programming Project			   */

package gokul.cs3200.rsr;

import gokul.cs3200.rsr.R;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.webkit.Plugin;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

/*
 * This class is used to create the displayRSSItems Activity which is triggered when an item in the
 * ListView is selected.
 */

public class RSSItemDisplayer extends Activity {
	
	WebView browser;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rss_item_displayer);

		this.setTitle(gokul.cs3200.rsr.ReallySimpleReader.RSSTitle);
		RSSItem selectedRssItem = gokul.cs3200.rsr.ReallySimpleReader.selectedItem;

		TextView titleTv = (TextView) findViewById(R.id.titleTextView);

		SimpleDateFormat sdf = new SimpleDateFormat("EEE d MMM \t hh:mm aa");
		String title = "";
		title = "\n" + selectedRssItem.getTitle() + "\n\t\t"
				+ sdf.format(selectedRssItem.getPubDate()) + " \n";

		String content = "";
		content += "\n" + selectedRssItem.getDescription() + "\n"
				+ selectedRssItem.getLink();

		titleTv.setText(title);
		
		 browser=(WebView)findViewById(R.id.webkit);
		 browser.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		 browser. getSettings().setJavaScriptEnabled (true);
		 browser. getSettings().setPluginsEnabled (true);
		 browser. getSettings().setSupportZoom (false);
		 browser. setVerticalScrollBarEnabled (true);
		 browser. setHorizontalScrollBarEnabled (false);
		 
		    
		
		            
		   // browser.loadUrl("file:///android_asset/index.html");
		    
		    
		
		    
		    
		     InputStream input = null;
			try {
				input = getAssets().open("index.html");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	            // myData.txt can't be more than 2 gigs.
	            int size = 0;
				try {
					size = input.available();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            byte[] buffer = new byte[size];
	            try {
					input.read(buffer);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            try {
					input.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	 
	            // byte buffer into a string
	            String text = new String(buffer);


			    
			    text=text.replace("BODY_OF_ARTICLE_YAN", selectedRssItem.getDescription());
			    text=text.replace("TITLE_OF_ARTICLE_YAN", selectedRssItem.getTitle());
			    text=text.replace("TIME_OF_ARTICLE_YAN",  sdf.format(selectedRssItem.getPubDate()));
			    
			    String page="<html><body>"
	            +text
	            +"</body></html>";
	    
			    
			    Log.d("DESCIPTION", selectedRssItem.getDescription());

	    
		          browser.loadDataWithBaseURL("x-data://base", page,
		                                        "text/html", "UTF-8",
		                                        null);
	}
	
	
	
	
	
	  void loadTime() {
		
		  }

		  private class Callback extends WebViewClient {
		    public boolean shouldOverrideUrlLoading(WebView view, String url) {
		      loadTime();
		      
		      return(true);
		    }
		  }
		
}